package com.xpsoft.oa.workflow.event;

import java.math.BigDecimal;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;
import org.jbpm.api.model.OpenProcessInstance;

import com.xpsoft.core.util.AppUtil;
import com.xpsoft.oa.model.admin.AdminExpenses;
import com.xpsoft.oa.model.flow.ProcessRun;
import com.xpsoft.oa.model.system.AppUser;
import com.xpsoft.oa.service.admin.AdminExpensesService;
import com.xpsoft.oa.service.flow.ProcessRunService;
import com.xpsoft.oa.service.system.AppUserService;
import com.xpsoft.oa.service.system.DepartmentService;

public class AdminExpensesFinishListener implements EventListener{
	private Log logger=LogFactory.getLog(AdminExpensesFinishListener.class);
	@Override
	public void notify(EventListenerExecution execution) throws Exception {
		//获取表单参数
		OpenProcessInstance pi=execution.getProcessInstance();
		String depId = (String)pi.getVariable("adminExpenses.dept.depId");
		String applyUserId = (String)pi.getVariable("adminExpenses.applyUser.userId");
		Date formDate = (Date)pi.getVariable("adminExpenses.formDate");
		String expensesType = (String)pi.getVariable("adminExpenses.expensesType");
		String dataList = (String)pi.getVariable("adminExpenses.dataList");
//		BigDecimal amount = (BigDecimal)pi.getVariable("adminExpenses.amount");
//		String amountBig = (String)pi.getVariable("adminExpenses.amountBig");
		BigDecimal leanAmount = (BigDecimal)pi.getVariable("adminExpenses.leanAmount");
		String isToLean = (String)pi.getVariable("adminExpenses.isToLean");
		String remark = (String)pi.getVariable("adminExpenses.remark");
		
		AdminExpenses adminExpenses = new AdminExpenses();
		adminExpenses.setFormDate(formDate);
		adminExpenses.setExpensesType(expensesType);
//		adminExpenses.setAmount(amount);
//		adminExpenses.setAmountBig(amountBig);
		adminExpenses.setLeanAmount(leanAmount);
		adminExpenses.setIsToLean(isToLean);
		adminExpenses.setRemark(remark);
		
		if(StringUtils.isNotBlank(depId)){
			DepartmentService departmentService = (DepartmentService)AppUtil.getBean("departmentService");
			adminExpenses.setDept(departmentService.get(Long.valueOf(depId)));
		}
		
		if(StringUtils.isNotBlank(applyUserId)){
			AppUserService appUserService = (AppUserService)AppUtil.getBean("appUserService");
			adminExpenses.setApplyUser(appUserService.get(Long.valueOf(applyUserId)));
		}
		
		ProcessRunService processRunService=(ProcessRunService)AppUtil.getBean("processRunService");
		ProcessRun processRun=processRunService.getByExeId(pi.getId());
		adminExpenses.setProcessRunId(processRun.getRunId());
		
		AdminExpensesService adminExpensesService = (AdminExpensesService)AppUtil.getBean("adminExpensesService");
		adminExpensesService.saveAdminExpenses(adminExpenses,dataList);
		
		
		//获取流程发起者对象
		AppUser flowStartUser=(AppUser)execution.getProcessInstance().getVariable("flowStartUser");
	
		//所有参与流程的都发消息
		AppUtil.sendSysShortMsg(execution.getProcessInstance().getId(), flowStartUser);
	}

}
